﻿Imports SpatialAlbum.WPF.EnumPage

Public Enum EnumPage
    eMain
    eIntroduction
    eConnection
    ePhotos
    eAlbums
    eDatabaseTools
    eImageLoader
    eSqlInspector
    eLog
    eOptions
End Enum

Public Class PageManager

    Private Shared m_pages As Dictionary(Of EnumPage, Page)


    Private Shared Sub Initialize()
        m_pages = New Dictionary(Of EnumPage, Page)
        'Dim thr As New System.Threading.Thread(AddressOf PrimePages)
        'thr.Priority = System.Threading.ThreadPriority.BelowNormal
        'thr.Name = "PrimePageThread"
        'thr.SetApartmentState(System.Threading.ApartmentState.STA)
        'thr.Start()
    End Sub

    ' PROBLEM: cannot use this to initialize pages on a separate thread because
    ' that thread will end up owning the page instead of the main thread....
    Private Shared Sub PrimePages()
        If m_pages.ContainsKey(eImageLoader) Then Return
        Dim p As Page = New ImageLoaderPage
        Trace.WriteLine("PrimePages - created ImageLoaderPage")
        SyncLock m_pages
            Trace.WriteLine("PrimePages - got lock")
            If Not m_pages.ContainsKey(eImageLoader) Then
                Trace.WriteLine("PrimePages - storing ImageLoaderPage")
                m_pages.Item(eImageLoader) = p
            Else
                Trace.WriteLine("PrimePages - not storing - already set")
            End If
        End SyncLock
        Trace.WriteLine("PrimePages - released lock")
        Trace.WriteLine("PrimePages - done")
    End Sub

    Public Shared Function Page(ByVal pagetype As EnumPage)
        If m_pages Is Nothing Then Initialize()
        Dim p As Page = Nothing
        Dim redirect_to_connection As Boolean = False
        If My.Settings.RequireConnection Then
            If (g_connection Is Nothing OrElse Not g_connection.Connected) And _
               (pagetype = eAlbums Or pagetype = eDatabaseTools Or _
                pagetype = eImageLoader Or pagetype = ePhotos) Then
                redirect_to_connection = True
            End If
        End If
        Dim requested_pagetype As EnumPage = pagetype
        If redirect_to_connection Then pagetype = eConnection

        If m_pages.ContainsKey(pagetype) Then
            p = m_pages.Item(pagetype)
        Else
            Select Case pagetype
                Case eMain : p = New MainPage
                Case eConnection : p = New ConnectionPage
                Case eAlbums : p = New AlbumsPage
                Case eDatabaseTools : p = New DatabaseToolsPage
                Case eImageLoader : p = New ImageLoaderPage
                Case eIntroduction : p = New IntroductionPage
                Case eLog : p = New LogPage
                Case eOptions : p = New OptionsPage
                Case ePhotos : p = New PhotosPage
                Case eSqlInspector : p = New SqlInspectorPage
                Case Else : AssertNotReached("PageManager.Page: unhandled EnumPage value")
            End Select
            If p IsNot Nothing Then m_pages.Add(pagetype, p)
        End If
        Try
            Dim conn As ConnectionPage = CType(p, ConnectionPage)
            conn.Redirected = redirect_to_connection
            conn.GoToPageOnConnect = requested_pagetype
        Catch
        End Try
        Return p
    End Function

End Class
